<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/9/13
 * Time: 13:09
 */

namespace App\HttpController\Home;


use App\Enums\UrlEnums;
use App\Library\Auth;
use App\Utility\Pool\MysqlObject;
use App\Utility\Pool\MysqlPool;
use EasySwoole\HttpClient\HttpClient;

class Player extends Base
{
    /**
     * 选手联赛统计
     * @return bool
     * @throws \EasySwoole\HttpClient\Exception\InvalidUrl
     */
    public function leagueStats()
    {
        $params = $this->request()->getQueryParams();
        if( !isset($params['league_id']) ){
            return $this->writeJson(0,null,'缺少league_id参数');
        }
        if( !isset($params['player_id']) ){
            return $this->writeJson(0,null,'缺少player_id参数');
        }
        if( !isset($params['game']) ){
            return $this->writeJson(0,null,'缺少game参数');
        }
        if( $params['game'] == 'lol' ){
            $url = UrlEnums::FJURL.UrlEnums::LOL_PYLEA_STATS_PATH."?league_id={$params['league_id']}&player_id={$params['player_id']}&version=2";
            $client = new HttpClient($url);
            list($s1, $s2) = explode(' ', microtime());
            $microtime = (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000); //毫秒时间戳
            $option = [
                'path'      => UrlEnums::LOL_PYLEA_STATS_PATH,
                'api_time'  => $microtime
            ];
        }else{
            return $this->writeJson(0,null,'暂无统计');
        }

        $header = Auth::getInstance()->header($option);
        $client->setHeaders($header,true,false);
        $response = $client->get();
        $content = $response->getBody();
        $data = json_decode($content,true);
        if( isset($data['code']) && $data['code'] == 200 && $data['data'] ){
            return $this->writeJson(1,$data['data'],'请求成功');
        }else{
            return $this->writeJson(0,null,$data['message']);
        }
    }

    /**
     * 选手综合资料
     * @return bool
     * @throws \EasySwoole\HttpClient\Exception\InvalidUrl
     */
    public function compositeStats()
    {
        $params = $this->request()->getQueryParams();
        if( !isset($params['player_id']) ){
            return $this->writeJson(0,null,'缺少player_id参数');
        }
        if( !isset($params['game']) ){
            return $this->writeJson(0,null,'缺少game参数');
        }
        if( $params['game'] == 'lol' ){
            $url = UrlEnums::FJURL.UrlEnums::LOL_PLAYER_COMPOSITE_STAS."?player_id={$params['player_id']}&version=2";
            $client = new HttpClient($url);
            list($s1, $s2) = explode(' ', microtime());
            $microtime = (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000); //毫秒时间戳
            $option = [
                'path'      => UrlEnums::LOL_PLAYER_COMPOSITE_STAS,
                'api_time'  => $microtime
            ];
        }else{
            return $this->writeJson(0,null,'暂无统计');
        }

        $header = Auth::getInstance()->header($option);
        $client->setHeaders($header,true,false);
        $response = $client->get();
        $content = $response->getBody();
        $data = json_decode($content,true);
        if( isset($data['code']) && $data['code'] == 200 && $data['data'] ){
            return $this->writeJson(1,$data['data'],'请求成功');
        }else{
            return $this->writeJson(0,null,$data['message']);
        }
    }

    /**
     * 选手的基本信息
     * @return bool
     * @throws \EasySwoole\Component\Pool\Exception\PoolEmpty
     * @throws \EasySwoole\Component\Pool\Exception\PoolException
     * @throws \Throwable
     */
    public function info()
    {
        $params= $this->request()->getQueryParams();
        if( !isset($params['player_id']) ){
            return $this->writeJson(0,null,'缺少player_id参数');
        }
        if( !isset($params['game']) ){
            return $this->writeJson(0,null,'缺少game参数');
        }
        $player = MysqlPool::invoke(function (MysqlObject $db)use($params){
            $table = "siam_{$params['game']}player";
            return $db->where('player_id',$params['player_id'])->getOne($table);
        });
        return $this->writeJson(1,$player,'请求成功');
    }

    /**
     * 选手的英雄信息
     */
    public function heroData()
    {
        $params = $this->request()->getQueryParams();
        if( !isset($params['player_id']) ){
            return $this->writeJson(0,null,'缺少player_id参数');
        }
        if( !isset($params['game']) ){
            return $this->writeJson(0,null,'缺少game参数');
        }
        if( $params['game'] == 'dota' ){
            $url = UrlEnums::FJURL.UrlEnums::DOTA_HERO_DATA."?player_id={$params['player_id']}&version=2";
            $client = new HttpClient($url);
            list($s1, $s2) = explode(' ', microtime());
            $microtime = (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000); //毫秒时间戳
            $option = [
                'path'      => UrlEnums::DOTA_HERO_DATA,
                'api_time'  => $microtime
            ];
        }else{
            return $this->writeJson(0,null,'暂无统计');
        }
        $header = Auth::getInstance()->header($option);
        $client->setHeaders($header,true,false);
        $response = $client->get();
        $content = $response->getBody();
        $data = json_decode($content,true);
        if( isset($data['code']) && $data['code'] == 200 && $data['data'] ){
            return $this->writeJson(1,$data['data'],'请求成功');
        }else{
            return $this->writeJson(0,null,$data['message']);
        }
    }

}